package com.CustomClasses;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;
import android.widget.ProgressBar;

import com.bolao.MainTabActivity;
import com.entidades.News;

public class FetchImage extends AsyncTask<Object,Void,Bitmap> {

	 private ProgressBar pbar;
	 private News news;
	public FetchImage(ProgressBar pbar) {
		this.pbar = pbar;
	}
	
	public FetchImage(){
		pbar = new ProgressBar(MainTabActivity.getmContext());
	}
	
	@Override
	protected void onPreExecute() {
		pbar.setVisibility(View.VISIBLE);
	};

	@Override
	protected Bitmap doInBackground(Object... params) {
		try {
			news = (News) params[0];
			
	        java.net.URL url = new java.net.URL(news.getmEnclosure());
	        HttpURLConnection connection = (HttpURLConnection) url
	                .openConnection();
	        connection.setDoInput(true);
	        connection.connect();
	        InputStream input = connection.getInputStream();
	        Bitmap myBitmap = BitmapFactory.decodeStream(input);
	        return myBitmap;
	    } catch (IOException e) {
	        e.printStackTrace();
	        return null;
	    }
	}
	
    @Override
    protected void onPostExecute(Bitmap eventImage) {
        if(eventImage!=null){                    
            news.setImage(eventImage);
            pbar.setVisibility(View.GONE);
        }
    }  
	





}
